<template>
  <div>
    <div id="header">
      <div class="left-box">
        <div class="logo">
          <span class="iconfont icon-music"></span><span>Music</span>
        </div>
        <div class="navigation-box">
          <i @click="goBack" class="iconfont icon-fenxiang back"></i>
          <i @click="goForward" class="iconfont icon-right forward"></i>
        </div>
        <div class="search-bar">
          <i class="el-icon-search"></i>
          <span>搜索推荐</span>
        </div>
      </div>
      <div class="right-box">
        <div class="avatar-box">
          <img
            v-if="userInfo.nickname === undefined"
            src="./images/default_avatar.png"
            alt="avatar"
          />
          <img
            v-if="userInfo.nickname !== undefined"
            :src="userInfo.avatarUrl"
            alt="avatar"
          />
        </div>
        <div class="username" v-if="userInfo.nickname !== undefined">
          {{ userInfo.nickname }}
        </div>
        <el-link
          v-if="userInfo.nickname === undefined"
          type="primary"
          class="login-text"
          @click="showLogin"
          >登录</el-link
        >
        <div class="down-button"><i class="el-icon-caret-bottom"></i></div>
      </div>
    </div>
    <!-- 登录组件 -->
    <transition>
      <Login />
    </transition>
  </div>
</template>

<script>
import Login from "@/components/Login";
import { mapState } from "vuex";
export default {
  name: "Header",
  data() {
    return {};
  },
  methods: {
    showLogin() {
      this.$store.dispatch("showLogin", true);
    },
    // 历史后退
    goBack(){
      this.$router.back()
    },
    // 历史前进
    goForward(){
      this.$router.forward()
    }
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
    }),
  },
  components: {
    Login,
  },
};
</script>

<style lang="less">
#header {
  position: fixed;
  width: 100%;
  background: var(--themeColor1);
  padding: 15px;
  height: 28px;
  display: flex;
  color: rgba(255, 255, 255, 0.7);
  align-content: center;
  justify-content: space-between;
  z-index: 101;
  .left-box {
    display: flex;
    .logo {
      color: #fff;
      span {
        font-size: 20px;
      }
      .iconfont {
        margin-right: 10px;
      }
    }
    .navigation-box {
      margin-left: 100px;
      display: flex;
      align-items: center;
      i {
        background: rgba(0, 0, 0, 0.1);
        border-radius: 50%;
        padding: 5px;
        cursor: pointer;
      }
      i:hover {
        transition: 300ms;
        transform: scale(1.1);
      }
      .forward {
        margin-left: 10px;
      }
    }
    .search-bar {
      width: 200px;
      height: 28px;
      margin-left: 10px;
      background: rgba(80, 80, 80, 0.1);
      border-radius: 56px;
      i {
        margin-left: 10px;
        font-size: 14px;
        vertical-align: -2px;
      }
      span {
        font-size: 12px;
        margin-left: 10px;
        line-height: 28px;
      }
    }
  }
  .right-box {
    display: flex;
    align-content: center;
    .avatar-box {
      height: 28px;
      width: 28px;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
      }
    }
    .username {
      margin-left: 10px;
      font-size: 12px;
      line-height: 28px;
    }
    .login-text {
      color: rgba(255, 255, 255, 0.7);
      margin-left: 10px;
      font-size: 12px;
      line-height: 28px;
    }
    .down-button {
      margin-left: 5px;
      i {
        vertical-align: -3px;
      }
    }
  }
}
</style>